package com.woniu.crowd.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.crowd.app.info.SelectLoGInfo;
import com.woniu.crowd.app.info.SelelctOpinion;
import com.woniu.crowd.client.SelectQueryClient;
import com.woniu.crowd.client.param.ListApplyParam;
import com.woniu.crowd.client.param.ListSelectParam;
import com.woniu.crowd.client.param.SearchSelectParam;
import com.woniu.crowd.infra.model.SelectApply;
import com.woniu.crowd.infra.model.SelectLog;
import com.woniu.crowd.infra.model.SelectProduct;
import com.woniu.crowd.infra.model.UserOpinion;
import com.woniu.crowd.service.SelectApplyService;
import com.woniu.crowd.service.SelectLogService;
import com.woniu.crowd.service.SelectProductService;
import com.woniu.crowd.service.UserOpinionService;
import com.woniu.crowd.service.form.ListApplyForm;
import com.woniu.crowd.service.form.SearchSelectForm;
import com.woniu.product.client.ProductQueryClient;
import com.woniu.product.client.param.ListCrowdProductParam;
import com.woniu.product.client.result.SpuResultInfo;
import com.woniu.result.Result;
import com.woniu.user.client.UserInfoClient;
import com.woniu.user.client.result.UserResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class SelectQueryApiImpl implements SelectQueryClient {
    @Resource
    private ProductQueryClient productQueryClient;
    @Resource
    private  SelectProductService selectProductService;
    @Resource
    private SelectApplyService selectApplyService;;
    @Resource
    private SelectLogService selectLogService;
    @Resource
    private UserInfoClient userInfoClient;
    @Resource
    private UserOpinionService userOpinionService;
    @Override
    public Result manageListSelect(ListSelectParam listSelectParam) {
        log.info("平台分页查询众筹商品：{}",listSelectParam);
        ListCrowdProductParam listCrowdProductParam = BeanUtil.copyProperties(listSelectParam, ListCrowdProductParam.class);
        return productQueryClient.pageListProductCrowd(listCrowdProductParam);
    }

    @Override
    public Result searchSelect(SearchSelectParam searchSelectParam) {
        log.info("搜索甄选商品:{}",searchSelectParam);
        SearchSelectForm searchSelectForm = BeanUtil.copyProperties(searchSelectParam, SearchSelectForm.class);
        return selectProductService.searchCrowd(searchSelectForm);
    }

    @Override
    public Result selAllApply(ListApplyParam listApplyParam) {
        log.info("平台分页查询甄选申请：{}",listApplyParam);
        ListApplyForm listApplyForm = BeanUtil.copyProperties(listApplyParam, ListApplyForm.class);

        return selectApplyService.selAllApply(listApplyForm);
    }

    @Override
    public Result sellog(Integer userId) {
        log.info("查询用户的甄日志：{}",userId);
        List<SelectLog> logs = selectLogService.list(new QueryWrapper<SelectLog>().eq("user_id", userId));
        List<SelectLoGInfo> selectLoGInfos = new ArrayList<>();
        for (SelectLog selectLog : logs) {
            SelectLoGInfo selectLoGInfo = new SelectLoGInfo();
            Result<SpuResultInfo> spu = productQueryClient.getSpuInfoBySpuId(selectLog.getSelectProductId(),-1);
            selectLoGInfo.setSpuName(spu.getData().getSpuName());
            SelectApply apply = selectApplyService.getById(selectLog.getApplyId());
            selectLoGInfo.setApplyStatus(apply.getApplyStatus());
            selectLoGInfos.add(selectLoGInfo);
        }
        return Result.getInstance(List.class).setMsg("查询成功").setData(selectLoGInfos);
    }

    @Override
    public Result selOpinion(Integer selectId) {
        UserOpinion opinion = userOpinionService.getOne(new QueryWrapper<UserOpinion>().eq("select_id", selectId));
        Result<UserResultInfo> userResultInfoResult = userInfoClient.searchUserInfo(opinion.getUserId());
        SelectProduct selelctinfo = selectProductService.getById(selectId);

        SelectProduct selectProduct = selectProductService.getById(selectId);
        Result<SpuResultInfo> spuInfoBySpuId = productQueryClient.getSpuInfoBySpuId(selectProduct.getProductId(),-1);
        SelelctOpinion selelctOpinion = new SelelctOpinion();
        selelctOpinion.setUserNickname(userResultInfoResult.getData().getUserNickname());
        selelctOpinion.setSpuName(spuInfoBySpuId.getData().getSpuName());
        return Result.getInstance(SelelctOpinion.class).setData(selelctOpinion);
    }


}
